/** @jsxImportSource @emotion/react */
import * as React from "react";
import {Button, TextArea, Text, useToast, InputLine, Input,} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, useItemInputControl} from "../../common/base";
import {管道级别} from "../../../device/edit/压力管道";
import {useMedia} from "use-media";
import {assertNamesUnique} from "../../common/eHelper";
import {EditStorageContext} from "../../StorageContext";
import {render施工类别, 证书除概况} from "./CertificatePage";
import {itemA下结论} from "../../gas/editorJj";
import {施工情况选} from "../../gas/ldistanceJj/SiteConstruct";
import {itemA焊接资格} from "../../gas/ldistanceJj/WeldingQualif";
import {itemA无损资格} from "../../gas/ldistanceJj/NonDestructive";
import {itemA工作内容} from "./JobResponsibili";
import {itemA问题处} from "../../gas/ldistanceJj/ProblemHandle";
import {itemA质保体} from "./QualityAssure";
import {itemA单特性} from "../EditorFeature";


//【管道特殊】 同一时间对一个设备允许出现多个监检报告的，若是台账的字段估计不能通用的。
const 管道级选=['详见管道数据表' ,...管道级别];
const input许可编=(name:string,desc:string)=>{
    return {
        edit:(inp:any,setInp:(a:any)=>void,orc:any)=>{
            return <div><Text css={{fontSize:'0.75rem'}}>{desc}{'>>'}</Text>
                <InputLine  label='许可证编号'>
                    <Input  value={inp?.[name] ||''}
                            onChange={e => setInp({ ...inp, [name]: e.currentTarget.value||undefined}) } />
                </InputLine>
            </div>;
        },
    };
};
export const config设备概况 = [
    [['工程名称', {n:'工程名称',t:'B',l:[]}], ],
    [['工程地址', {n:'工程地址',t:'B' }] ],
    [['工程总长度',{n:'总长度', u:'m'}], ['管道级别', {n:'管道级别',t:'l',l:管道级选}] ],
    [['设计压力',{n:'设压力', u:'MPa'}], ['输送介质', {n:'输介质',t:'l',l:['天然气',]}] ],
    [['开工时间',{n:'开工时',t:'d'}], ['竣工时间',{n:'竣工时',t:'d'}] ],
    [['建设单位','_$使用单位']],
    [['设计单位','设单位'], ['许可证编号','设许号',input许可编('设许号','设计单位')] ],
    [['施工单位','施单位'], ['许可证编号','施许号',input许可编('施许号','施工单位')] ],
    [['检测机构','测机构'], ['核准证编号','检证号']],
    [['施工类别', {}, render施工类别],  ['授权监督检验文号','检受理号'] ],
];

export const tail监检工 = <Text css={{"@media print": {fontSize: '0.75rem'}}}>
    “检验方法”可填写“Z”表示资料确认，“X”表示现场监督，“W”表示实物抽查。如有问题，将存在的问题记录在备注栏。
</Text>;

/**开启编制时的：默认值初始化操作。  若打印：本编辑器全部都不显示。
 * */
export const EntranceSetup =
React.forwardRef((
    {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
) => {
    const atPrint = useMedia('print');
    const toast = useToast();
    const doCheckNames = React.useCallback((verId: string) => {
        //config证书概要 有冲突交叉的情形：【设备排除】取代不重复的，然后 config设备概况 就省略；
        const result = assertNamesUnique([{value: rep?.tzFields},
            {value: config设备概况, type:'esnt'}, {value: 证书除概况, type:'esnt'},
            {value:[...itemA下结论,  ...施工情况选,...itemA焊接资格,...itemA无损资格,
                    ...itemA工作内容, ...itemA问题处,  ...itemA质保体, ...itemA单特性, ]},
            {value:['Projects', ]} ]);
        toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
    }, [rep,toast]);
    const {storage, setStorage} =React.useContext(EditStorageContext) as any;
    const getInpFilter = React.useCallback((par: any) => {
        const {_tblFixed, } =par||{};
        return {_tblFixed, };
    }, []);
    const {inp, setInp} = useItemInputControl({ref});
    if (atPrint) return null;
    else return (
        <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                             alone={alone} label={'初始化本报告，默认值配置等'}>
            {process.env.REACT_APP_TEST==='true' && <><div>
                <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
                <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
                <Text variant="h5">设置待测试表格的各列宽度：</Text>
                <TextArea value={storage?._tblFixed || ''} rows={2} onChange={e =>{
                              JSON.parse(e.currentTarget.value??'[]');
                              setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}); }}/>
            </div>
            </>
            }
            <hr/>
        </InspectRecordLayout>
    );
});
